package com.linkoog.devtools.hide_project_path;

import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.ProjectRootsUtil;
import com.intellij.ide.projectView.impl.nodes.ProjectViewDirectoryHelper;
import com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.ui.SimpleTextAttributes;
import com.linkoog.devtools.hide_project_path.persistent.ShowProjectPathSettings;

import java.util.Objects;



public class PsiDirectoryNodeCustom extends PsiDirectoryNode {
    public PsiDirectoryNodeCustom(Project project, PsiDirectory value, ViewSettings viewSettings) {
        super(project, value, viewSettings);
    }

    @Override
    protected void updateImpl(PresentationData data) {
        Project project = this.getProject();
        if (project == null) return;

        PsiDirectory psiDirectory = this.getValue();
        if (psiDirectory == null) return;

        final ProjectViewDirectoryHelper directoryHelper = ProjectViewDirectoryHelper.getInstance(project);

        VirtualFile directoryFile = psiDirectory.getVirtualFile();
        Object parentValue = this.getParentValue();
        if (ProjectRootsUtil.isModuleContentRoot(directoryFile, project)) {
            ProjectFileIndex name = ProjectRootManager.getInstance(project).getFileIndex();
            Module module = name.getModuleForFile(directoryFile);
            data.setPresentableText(directoryFile.getName());
            if (module != null) {
                if (!(parentValue instanceof Module)) {
                    if (!this.shouldShowModuleName()) {
                        data.addText(directoryFile.getName() + " ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                    } else if (Objects.equals(module.getName(), directoryFile.getName())) {
                        data.addText(directoryFile.getName(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
                    } else {
                        data.addText(directoryFile.getName() + " ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
                        data.addText("[" + module.getName() + "]", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
                    }
                } else {
                    data.addText(directoryFile.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
                }

                boolean shouldShowUrl = ShowProjectPathSettings.getInstance(project).isShowProjectPath();
                data.setLocationString(directoryHelper.getLocationString(psiDirectory, shouldShowUrl, this.shouldShowSourcesRoot()));
                this.setupIcon(data, psiDirectory);
                return;
            }
        }

        String name;
        if (parentValue instanceof Project){
            name = psiDirectory.getVirtualFile().getPresentableUrl();
        } else {
            name = directoryHelper.getNodeName(this.getSettings(), parentValue, psiDirectory);
        }


        if (name == null) {
            this.setValue(null);
        } else {
            data.setPresentableText(name);
            data.setLocationString(directoryHelper.getLocationString(psiDirectory, false, false));
            this.setupIcon(data, psiDirectory);
        }
    }
}